package com.bawei.module_mine.ui

import android.content.pm.PackageManager
import android.view.View
import android.widget.Toast
import androidx.core.app.ActivityCompat
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_mvi.Router
import com.bawei.lib_mvi.base.BaseActivity
import com.bawei.module_mine.databinding.ActivityPhoneBookBinding
import com.bawei.module_mine.model.GetPermissions
import android.Manifest
import com.alibaba.android.arouter.facade.annotation.Route

@Route(path = Router.Ui.PHONE_BOOK_ACTIVITY)
class PhoneBookActivity : BaseActivity() {

    private val binding by lazy {
        ActivityPhoneBookBinding.inflate(layoutInflater)
    }

    override fun getLayoutRootView(): View {
        return binding.root
    }

    override fun initView() {
        super.initView()
        binding.tvJump.setOnClickListener {
            ARouter.getInstance().build(Router.Ui.MainActivity).navigation()
        }
        binding.btnPhoneBook.setOnClickListener{
            GetPermissions.requestAllPermissions(this)
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 100){
            var allGranted = true
            for (result in grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    allGranted = false
                    break
                }
            }
            
            if (allGranted) {
                Toast.makeText(this, "所有权限已开启", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "部分权限未授权，可能影响应用功能", Toast.LENGTH_SHORT).show()
            }
            ARouter.getInstance().build(Router.Ui.MainActivity).navigation()
        }
    }

}